package com.cjg.gulimall.product.service.impl;

import com.cjg.gulimall.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjg.common.utils.PageUtils;
import com.cjg.common.utils.Query;

import com.cjg.gulimall.product.dao.CategoryDao;
import com.cjg.gulimall.product.entity.CategoryEntity;
import com.cjg.gulimall.product.service.CategoryService;
import org.springframework.transaction.annotation.Transactional;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {
    //ServiceImpl是mybatis提供的方法内容再下面
// public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T>
    //M 的继承就是泛型的约束 就是这里对应的CategoryDao 所以 BaseMapper就是CategoryDao
//    @Autowired  原始写法
//    CategoryDao categoryDao;
    @Autowired
    CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
        //查出所有分类
        List<CategoryEntity> entities = baseMapper.selectList(null);


        //组装成父子的树形结构
//        1.找到所以一级分类,parentid==0
        List<CategoryEntity> collect = entities.stream().filter(x ->
                x.getParentCid() == 0
        ).map((menu) -> {
            //将获取的一级分类下的所有子分类递归聚合
            menu.setChildren(getChildren(menu, entities));
            return menu;
        }).sorted((m1, m2) -> {
//            数据库对应实体类Sort为Integer类型可能为null,所以必须进行判断

            return (m1.getSort()==null?0:m1.getSort())-(m2.getSort()==null?0:m2.getSort());
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public void removeMenuId(List<Long> asList) {
        //todo //检查当前菜单是否被别的地方引用

        //项目中一般部直接删除 ,有标志位来替代,逻辑删除
        /**
         *
         */
        baseMapper.deleteBatchIds(asList);

    }

    @Override
    public Long[] findCatelogPath(Long catelogId) {

        List<Long> path =new ArrayList<>();
        List<Long> path1 = findPath(catelogId, path);
        Collections.reverse(path1);

        return   path1.toArray(new Long[path1.size()]);

    }

    @Transactional
    @Override
    public void updateByIdRelation(CategoryEntity category) {
        this.updateById(category);
        categoryBrandRelationService.updateReBrand(category.getCatId(),category.getName());

    }

    private List<Long> findPath(Long catelogId,List<Long> path){
        path.add(catelogId);
        CategoryEntity byId = this.getById(catelogId);
        if(byId.getParentCid()!=0){
            findPath(byId.getParentCid(),path);}
        return path;
    }


    //传入当前菜单和 总菜单 ,我们在总里面找到当前的菜单的子菜单
    private List<CategoryEntity> getChildren(CategoryEntity there, List<CategoryEntity> all) {
//递归查找菜单的子菜单

        List<CategoryEntity> collect = all.stream().filter(x -> {
            //这里能到三层是因为数据库的设计 过滤
            return x.getParentCid() == there.getCatId();
        }).map(m->{
            //set里调用get 就把二级菜单的x.getParentCid() == there.getCatId();,there变成了二级参数
            //因为这是 过滤后的,只会会循环调用
            m.setChildren(getChildren(m,all));
            return m;
        }).sorted((m1,m2)->{
            return (m1.getSort()==null?0:m1.getSort())-(m2.getSort()==null?0:m2.getSort());
        }).collect(Collectors.toList());

        return collect;
    }

}